package sk.tramtatiri.pages.users;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import ofc4j.model.Chart;
import ofc4j.model.axis.Label.Rotation;
import ofc4j.model.axis.XAxis;
import ofc4j.model.axis.XAxisLabels;
import ofc4j.model.axis.YAxis;
import ofc4j.model.elements.AreaHollowChart;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.tramtatiri.MySession;
import sk.tramtatiri.OpenFlashChart;
import sk.tramtatiri.data.dao.interfaces.FriendshipDao;
import sk.tramtatiri.data.dataobjects.User;
import sk.tramtatiri.data.dataobjects.UserInfo;

public class FriendStatisticsPanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1924714566860506670L;

	@SpringBean
	private FriendshipDao friendshipDao;
	
	private List<User> friendList = new ArrayList<User>();
	
	private List<UserInfo> userInfos;
	private String[] dates;
	private List<Number> weights;
	private int maxWeight;
	private int i;
	
	public FriendStatisticsPanel(String id) {
		super(id);
		
		friendList = friendshipDao.findAllFriedsSharingStats(((MySession)getSession()).getUserID()); 
		
		add(new ListView<User>("userRow", friendList) {
		    protected void populateItem(ListItem item) {
		    	AreaHollowChart hollow;
		    	Chart chart;
		    	OpenFlashChart openFlashChart;
		    	YAxis yaxis;
		    	XAxis xaxis;
		    	XAxisLabels xaxisLab;
		        final User user = (User) item.getModelObject();
		        item.add(new Label("userNick", user.getNickName()));
		        userInfos = user.getUserInfos();
		        dates = new String[userInfos.size()];
		        weights = new ArrayList<Number>();
		        maxWeight = 0;
		        i = 0;		        
				
		        for(UserInfo ui:userInfos) {
		        	weights.add(ui.getWeight());

					SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
					dates[i] = sdf.format(ui.getDate());

					if (ui.getWeight() >= maxWeight)
						maxWeight = ui.getWeight();
					
					i++;
		        }
		        
		        yaxis = new YAxis();
				yaxis.setSteps(20);
				xaxis = new XAxis();
				xaxisLab = new XAxisLabels();
				xaxisLab.setRotation(Rotation.VERTICAL);
				xaxisLab.addLabels(dates);
				xaxis.setXAxisLabels(xaxisLab);
				
		        hollow = new AreaHollowChart();
				hollow.addValues(weights);
				hollow.setColour("#007FFF");
				hollow.setTooltip("Vaha:#val#");
				hollow.setText("Vaha");
				hollow.setAlpha(0.1f);
				hollow.setFontSize(15);
				hollow.setDotSize(2);
				hollow.setHaloSize(1);
				
				chart = new Chart();
				chart.addElements(hollow);
				chart.setBackgroundColour("#FFFFFF");
				yaxis.setMin(0);
				yaxis.setMax(maxWeight + 10);
				chart.setYAxis(yaxis);
				chart.setXAxis(xaxis);

				openFlashChart = new OpenFlashChart("friendsChart", 500,
						250, chart);
				
				item.add(openFlashChart);
		    }
		});
	}

}
